package org.roger.search;

import java.io.IOException;
import java.util.List;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.WildcardQuery;
import org.roger.collector.FilePathCollector;
import org.roger.scorer.DistanceScorer;
import org.roger.search.filter.FileNameAccessor;
import org.roger.search.filter.FileNameFilter;
import org.roger.sort.DistanceComparatorSource;

/**
 * 通配符查询
 * @param field		查询域
 * @param wildcard	含有通配符的字符串(?匹配一个字符，*匹配0或者多个字符)
 */
public class WildcardSearch extends LuceneSearchBase{
	
	private String field;
	private String wildcard;
	
	public WildcardSearch(String field,String wildcard){
		this.field = field;
		this.wildcard = wildcard;
	}

	@Override
	public Query getSearchQuery() {
		WildcardQuery query = new WildcardQuery(new Term(field,wildcard));
		return query;
	}
	
	public static void main(String[] args) {
		WildcardSearch search = new WildcardSearch("name", "*1.txt*");
		try {
//			FilePathCollector collector = new FilePathCollector();
//			DistanceScorer scorer = new DistanceScorer(getReader("D:/index"));
//			collector.setScorer(scorer);
			search.search("E:/lucene/index");
//			List<String> paths = collector.getPaths();
//			List<Float> scorers = collector.getScorers();
//			for(int i = 0; i < paths.size(); i++){
//				System.out.println(paths.get(i)+"    "+scorers.get(i));
//			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Sort getSort() {
		Sort sort = new Sort(new SortField("location",new DistanceComparatorSource(0,0)));
		return sort;
	}
}
